/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.Instance;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.InviteInfo;
/*     */ import com.zimbra.cs.mailbox.calendar.ZOrganizer;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import java.io.IOException;
/*     */ import java.util.Collection;
/*     */ import java.util.Date;
/*     */ import java.util.Iterator;
/*     */ import javax.servlet.ServletOutputStream;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AtomFormatter
/*     */   extends Formatter
/*     */ {
/*     */   public void formatCallback(UserServletContext context)
/*     */     throws IOException, ServiceException
/*     */   {
/*  40 */     Iterator<? extends MailItem> iterator = null;
/*  41 */     StringBuffer sb = new StringBuffer();
/*  42 */     Element.XMLElement feed = new Element.XMLElement("feed");
/*  43 */     int offset = context.getOffset();
/*  44 */     int limit = context.getLimit();
/*     */     try {
/*  46 */       iterator = getMailItems(context, context.getStartTime(), context.getEndTime(), limit - offset);
/*     */       
/*  48 */       context.resp.setCharacterEncoding("UTF-8");
/*  49 */       context.resp.setContentType("application/atom+xml");
/*     */       
/*  51 */       sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
/*     */       
/*  53 */       feed.addAttribute("xmlns", "http://www.w3.org/2005/Atom");
/*     */       
/*  55 */       feed.addElement("title").setText("Zimbra " + context.itemPath);
/*  56 */       feed.addElement("generator").setText("Zimbra Atom Feed Servlet");
/*  57 */       feed.addElement("id").setText(context.req.getRequestURL().toString());
/*  58 */       feed.addElement("updated").setText(DateUtil.toISO8601(new Date(context.targetMailbox.getLastChangeDate())));
/*     */       
/*  60 */       int curHit = 0;
/*     */       
/*  62 */       while (iterator.hasNext()) {
/*  63 */         MailItem itItem = (MailItem)iterator.next();
/*  64 */         curHit++;
/*  65 */         if (curHit > limit)
/*     */           break;
/*  67 */         if (curHit >= offset) {
/*  68 */           if ((itItem instanceof CalendarItem))
/*     */           {
/*  70 */             CalendarItem calItem = (CalendarItem)itItem;
/*  71 */             if ((calItem.isPublic()) || (calItem.allowPrivateAccess(context.getAuthAccount(), context.isUsingAdminPrivileges())))
/*     */             {
/*  73 */               addCalendarItem(calItem, feed, context);
/*     */             }
/*  75 */           } else if ((itItem instanceof Message)) {
/*  76 */             addMessage((Message)itItem, feed);
/*     */           }
/*     */         }
/*     */       }
/*     */     } finally {
/*  81 */       if ((iterator instanceof Formatter.QueryResultIterator))
/*  82 */         ((Formatter.QueryResultIterator)iterator).finished();
/*     */     }
/*  84 */     sb.append(feed.toString());
/*  85 */     context.resp.getOutputStream().write(sb.toString().getBytes("UTF-8"));
/*     */   }
/*     */   
/*     */   public long getDefaultStartTime()
/*     */   {
/*  90 */     return System.currentTimeMillis() - 604800000L;
/*     */   }
/*     */   
/*     */ 
/*     */   public long getDefaultEndTime()
/*     */   {
/*  96 */     return System.currentTimeMillis() + 604800000L;
/*     */   }
/*     */   
/*     */   private void addCalendarItem(CalendarItem calItem, Element feed, UserServletContext context) throws ServiceException {
/* 100 */     Collection<CalendarItem.Instance> instances = calItem.expandInstances(context.getStartTime(), context.getEndTime(), false);
/* 101 */     for (Iterator<CalendarItem.Instance> instIt = instances.iterator(); instIt.hasNext();) {
/* 102 */       CalendarItem.Instance inst = (CalendarItem.Instance)instIt.next();
/* 103 */       InviteInfo invId = inst.getInviteInfo();
/* 104 */       Invite inv = calItem.getInvite(invId.getMsgId(), invId.getComponentId());
/* 105 */       Element entry = feed.addElement("entry");
/* 106 */       entry.addElement("title").setText(inv.getName());
/* 107 */       entry.addElement("updated").setText(DateUtil.toISO8601(new Date(inst.getStart())));
/* 108 */       entry.addElement("summary").setText(inv.getFragment());
/*     */       
/* 110 */       if (inv.hasOrganizer()) {
/* 111 */         Element author = entry.addElement("author");
/* 112 */         author.addElement("name").setText(inv.getOrganizer().getCn());
/* 113 */         author.addElement("email").setText(inv.getOrganizer().getAddress());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void addMessage(Message m, Element feed)
/*     */   {
/* 120 */     Element entry = feed.addElement("entry");
/* 121 */     entry.addElement("title").setText(m.getSubject());
/* 122 */     entry.addElement("summary").setText(m.getFragment());
/* 123 */     Element author = entry.addElement("author");
/*     */     
/* 125 */     author.addElement("name").setText(m.getSender());
/* 126 */     author.addElement("email").setText(m.getSender());
/* 127 */     entry.addElement("modified").setText(DateUtil.toISO8601(new Date(m.getDate())));
/*     */   }
/*     */   
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/* 132 */     return FormatterFactory.FormatType.ATOM;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/AtomFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */